package com.yuuzy.view;

import com.yuuzy.dao.InsuranceDAO;
import com.yuuzy.model.Insurance;
import com.yuuzy.utils.StringUtils;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class InsertInsurance extends JInternalFrame {
    private final ButtonGroup group = new ButtonGroup();
    private JTextField insurance_nameText;
    private JTextField insurance_projectText;
    private JTextField insurance_personText;
    private JTextField insurance_amountText;
    private JTextField insurance_yearText;
    private JRadioButton insurance_upRB;
    private JRadioButton insurance_downRB;
    private InsuranceDAO insuranceDAO = new InsuranceDAO();

    /**
     * Create the frame.
     */
    public InsertInsurance() {
        setIconifiable(true);
        setClosable(true);
        setTitle("添加保险产品");
        setBounds(100, 100, 763, 736);

        JLabel label = new JLabel("保险名称：");

        JLabel label_1 = new JLabel("保险项目：");

        JLabel label_2 = new JLabel("适用人群：");

        JLabel label_3 = new JLabel("保险金额：");

        JLabel label_4 = new JLabel("保险年限：");

        insurance_nameText = new JTextField();
        insurance_nameText.setColumns(10);

        insurance_projectText = new JTextField();
        insurance_projectText.setColumns(10);

        insurance_personText = new JTextField();
        insurance_personText.setColumns(10);

        insurance_amountText = new JTextField();
        insurance_amountText.setColumns(10);

        insurance_yearText = new JTextField();
        insurance_yearText.setColumns(10);

        JButton btnNewButton = new JButton(" 添加");
        btnNewButton.setIcon(new ImageIcon(InsertInsurance.class.getResource("/image/addition.png")));
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                insertActionPerformed(e);
            }
        });

        JButton btnNewButton_1 = new JButton(" 重置");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                resetValueActionPerformed(e);
            }
        });
        btnNewButton_1.setIcon(new ImageIcon(InsertInsurance.class.getResource("/image/reset.png")));

        JLabel lblNewLabel = new JLabel("是否上线：");

        insurance_upRB = new JRadioButton("是");
        insurance_upRB.setSelected(true);
        group.add(insurance_upRB);

        insurance_downRB = new JRadioButton("否");
        group.add(insurance_downRB);

        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(157)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addComponent(label_1)
                                                                .addGap(77)
                                                                .addComponent(insurance_projectText, GroupLayout.PREFERRED_SIZE, 223, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addComponent(label_2)
                                                                .addGap(77)
                                                                .addComponent(insurance_personText, GroupLayout.PREFERRED_SIZE, 224, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addComponent(label)
                                                                .addGap(77)
                                                                .addComponent(insurance_nameText, GroupLayout.PREFERRED_SIZE, 225, GroupLayout.PREFERRED_SIZE))))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(155)
                                                .addComponent(label_4)
                                                .addGap(81)
                                                .addComponent(insurance_yearText, GroupLayout.PREFERRED_SIZE, 224, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(155)
                                                .addComponent(label_3)
                                                .addGap(80)
                                                .addComponent(insurance_amountText, GroupLayout.PREFERRED_SIZE, 224, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(156)
                                                .addComponent(lblNewLabel)
                                                .addGap(79)
                                                .addComponent(insurance_upRB)
                                                .addGap(78)
                                                .addComponent(insurance_downRB))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(203)
                                                .addComponent(btnNewButton, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
                                                .addGap(100)
                                                .addComponent(btnNewButton_1, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)))
                                .addContainerGap(116, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(87)
                                                .addComponent(label)
                                                .addGap(58)
                                                .addComponent(label_1)
                                                .addGap(53)
                                                .addComponent(label_2)
                                                .addGap(55)
                                                .addComponent(label_3)
                                                .addGap(59)
                                                .addComponent(label_4)
                                                .addGap(63)
                                                .addComponent(lblNewLabel))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(80)
                                                .addComponent(insurance_nameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addGap(57)
                                                .addComponent(insurance_projectText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addGap(45)
                                                .addComponent(insurance_personText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addGap(50)
                                                .addComponent(insurance_amountText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addGap(56)
                                                .addComponent(insurance_yearText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                                                .addGap(49)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                                        .addComponent(insurance_upRB)
                                                        .addComponent(insurance_downRB))))
                                .addGap(76)
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addComponent(btnNewButton_1)
                                        .addComponent(btnNewButton))
                                .addContainerGap(88, Short.MAX_VALUE))
        );
        getContentPane().setLayout(groupLayout);

    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    InsertInsurance frame = new InsertInsurance();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 添加事件处理
     *
     * @param evt
     */
    private void insertActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String insurance_name = this.insurance_nameText.getText();
        String insurance_project = this.insurance_projectText.getText();
        String insurance_person = this.insurance_personText.getText();
        String insurance_amount = this.insurance_amountText.getText();
        String insurance_year = this.insurance_yearText.getText();

        if (StringUtils.isEmpty(insurance_name)) {
            JOptionPane.showMessageDialog(null, "保险名称不能为空！");
            return;
        }

        if (StringUtils.isEmpty(insurance_project)) {
            JOptionPane.showMessageDialog(null, "保险项目不能为空！");
            return;
        }

        if (StringUtils.isEmpty(insurance_person)) {
            JOptionPane.showMessageDialog(null, "适用人群不能为空！");
            return;
        }

        if (StringUtils.isEmpty(insurance_amount)) {
            JOptionPane.showMessageDialog(null, "保险金额不能为空！");
            return;
        }

        if (StringUtils.isEmpty(insurance_year)) {
            JOptionPane.showMessageDialog(null, "保险年限不能为空！");
            return;
        }


        String insurance_up = "";
        if (insurance_upRB.isSelected()) {
            insurance_up = "1";
        } else if (insurance_downRB.isSelected()) {
            insurance_up = "0";
        }

        if (StringUtils.isEmpty(insurance_up)) {
            JOptionPane.showMessageDialog(null, "请选择是否上线该保险！");
            return;
        }

        Insurance insurance = new Insurance(insurance_name, insurance_project, insurance_person, StringUtils.changeDouble(insurance_amount), StringUtils.changeInt(insurance_year), insurance_up);
        int insertNum = insuranceDAO.insertInsurance(insurance);
        if (insertNum > 0) {
            JOptionPane.showMessageDialog(null, "保险添加成功！");
            resetValue();
        } else {
            JOptionPane.showMessageDialog(null, "保险添加失败！");
        }
    }

    /**
     * 重置事件处理
     *
     * @param evt
     */
    private void resetValueActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        this.resetValue();
    }

    /**
     * 重置表单
     */
    private void resetValue() {
        this.insurance_nameText.setText("");
        this.insurance_projectText.setText("");
        this.insurance_personText.setText("");
        this.insurance_amountText.setText("");
        this.insurance_yearText.setText("");
        this.group.clearSelection();
    }
}
